package com.ke.jun.view.widght

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.ke.jun.view.R

class HeartView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet?=null, defStyleAttr: Int=0):
    View(context,attributeSet,defStyleAttr) {
    private val paint= Paint()
    private val path1=Path()
    private val path2=Path()

    private var heartColor: Int

    init {
        //获取属性
         val attr = context.obtainStyledAttributes(attributeSet, R.styleable.heartView)
          heartColor= attr.getColor(R.styleable.heartView_heartColor,Color.RED)
         attr.recycle()

        //初始化画笔
        with(paint){
             strokeWidth=5f
            isAntiAlias=true
            style=Paint.Style.FILL
            color=heartColor }




     }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //初始化path
        path1.moveTo((width/2).toFloat(), (height/4).toFloat());

        path1.cubicTo(((width*6)/7).toFloat(), (height/9).toFloat(), ((width*12)/13).toFloat(), ((height*2)/5).toFloat(), (width/2).toFloat(), ((height*7)/12).toFloat());

        path2.moveTo((width/2).toFloat(), (height/4).toFloat());

        path2.cubicTo((width / 7).toFloat(), (height / 9).toFloat(), (width / 13).toFloat(), ((height * 2) / 5).toFloat(), (width / 2).toFloat(), ((height * 7) / 12).toFloat());

        canvas?.drawPath(path1,paint)
        canvas?.drawPath(path2,paint)

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var widthMode = MeasureSpec.getMode(widthMeasureSpec)
        var heightMode = MeasureSpec.getMode(heightMeasureSpec)
        var widthSize = MeasureSpec.getSize(widthMeasureSpec)
        var heightSize = MeasureSpec.getMode(widthMeasureSpec)
        if (widthMode==MeasureSpec.AT_MOST&&heightMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(200,200)
        }else if (widthMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(200,heightSize)
        }else if (heightMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSize,200)
        }else{
            setMeasuredDimension(widthSize,heightSize)
        }


    }


}